package com.linkage.seapeak.activity.vote;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.Vote.ShowVoteMemberGroupAdapter;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.seapeak.base.BaseWhiteActivity;
import com.linkage.seapeak.pojo.CrediterUser;
import com.seapeak.recyclebundle.AYSwipeRecyclerView;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 展示表决成员 无分组
 */
public class ShowVoteMemberNoGroupActivity extends BaseWhiteActivity implements AYSwipeRecyclerView.OnRefreshLoadListener {

    private String projectId;
    private String voteId;

    private int voteRule;

    private AYSwipeRecyclerView mRecyclerView;
    private ShowVoteMemberGroupAdapter mDataAdapter;

    private List mData;

    int count = 0;
    int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_voters_no_group, "表决人员");
        projectId = getIntent().getStringExtra("projectId");
        voteId = getIntent().getStringExtra("voteId");
        voteRule = getIntent().getIntExtra("voteRule", -1);
        init();
        mRecyclerView.startLoadFirst();
    }

    /**
     * 重新计算，刷新
     */
    public void notifyDataChanged() {
        setTitle(mData.size() == 0 ? "表决人员" : "表决人员(" + count + ")");
        mRecyclerView.onFinishRequest(false, mData.size() < count);
    }

    public void init() {
        mRecyclerView = findViewById(R.id.list);
        mData = new ArrayList<>();
        mDataAdapter = new ShowVoteMemberGroupAdapter(ShowVoteMemberNoGroupActivity.this, mData);
        mRecyclerView.setAdapter(mDataAdapter);
        mRecyclerView.setOnRefreshLoadLister(this);
        mRecyclerView.setMode(AYSwipeRecyclerView.SwipeType.BOTH);

        mDataAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position, RecyclerView.ViewHolder viewHolder) {
//                switch (v.getId()) {
//                    case R.id.voter_change:
//                        moneyIndex = position;
//                        Intent intent = new Intent(ShowVoteMemberNoGroupActivity.this, VoteTurnMoneyActivity.class);
//                        intent.putExtra("money", ((CrediterUser) mData.get(moneyIndex)).getVoteMoney());
//                        startActivityForResult(intent, REQUEST_CODE_MONEY);
//                        break;
//                    case R.id.btnDelete:
//                        CrediterUser user = (CrediterUser) mData.get(position);
//                        deleteUser(user);
//                        break;
//                }
            }
        });
    }

    /**
     * 获取列表
     */
    private void getEditVoteMember() {
        Network.getAPIService()
                .getShowVoteMember(getUserToken(), projectId, voteId, "", voteRule == -1 ? "" : voteRule + "",page + "", "20"
                        )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            notifyDataChanged();
                        } else {
                            JsonObject jsonObject = json.getAsJsonObject("data").getAsJsonObject("data");
                            JsonArray groupArray = jsonObject.getAsJsonArray("list");
                            count = jsonObject.get("totalCount").getAsInt();

                            List<CrediterUser> crediterUsers = new ArrayList<>();
                            if (groupArray != null) {
                                for (int i = 0;i < groupArray.size();i++) {
                                    CrediterUser user = new Gson().fromJson(groupArray.get(i).toString(), CrediterUser.class);
                                    crediterUsers.add(user);
                                }
                            }
                            if (page == 1)
                                mData.clear();
                            page++;
                            mData.addAll(crediterUsers);
                            notifyDataChanged();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    @Override
    public void loadFirst() {
        page = 1;
        getEditVoteMember();
    }

    @Override
    public void loadNext() {
        getEditVoteMember();
    }

}
